<?php
/**
* LQueryBU DB Interface
*
* Light Query Business Unit Database Inetrface to PDO
 *  
 *
 * @package		LQueryBU_DB_Interface
 * @author		Xavier Perez <xmadamx@gmail.com>
 * @license 	GNU Lesser General Public License
 * @link		http://code.google.com/p/lquerybu/
 * @since		Version 1.0.1
 * @filesource
* 
*/

interface LQBU_DBI
{
	public function DBprepare($sql);
	public function DBexecute();
	public function DBfetchAll();
	public function DBrowCount();
	public function DBlastInsertId();
	public function DBcolumnCount();
	public function DBgetColumnMeta($column);
	public function DBbeginTransaction();
	public function DBcommit();
	public function DBqueryOne($sql);
	public function DBrollBack();	

}

class LQBU_DBC extends PDO implements LQBU_DBI
{
	private $link_ID;
	private $inTransaction = FALSE;
	
	function __construct($dbConnect)
	{
		$dbsocket 	= $dbConnect['socket'];
		$dbengine 	= $dbConnect['engine'];
		$dbhost 	= $dbConnect['host'];
		$dbport 	= $dbConnect['port'];
		$dbdatabase	= $dbConnect['database'];
		$dbuser		= $dbConnect['user'];
		$dbpass		= $dbConnect['pass'];
		$dbpersistent = $dbConnect['persistent'];
			
				
		if ($dbsocket == "") 
    	{
    		$dbdns = $dbengine.':host='.$dbhost.';dbname='.$dbdatabase;
    	}
    	else
    	{
    		$dbdns = $dbengine.':unix_socket='.$dbsocket.';dbname='.$dbdatabase;
    	}

    	try 
		{
        	parent::__construct($dbdns, $dbuser, $dbpass, array(PDO::ATTR_PERSISTENT => $dbpersistent));
		} 
		catch (Exception $e) 
		{
			throw $e;
		}
	}	
	
	public function DBprepare($sql)
	{
		$this->link_ID = $this->prepare($sql);
	}
	
	public function DBexecute()
	{
		return $this->link_ID->execute();
	}
	
	public function DBfetchAll()
	{
		return $this->link_ID->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function DBrowCount()
	{
		return $this->link_ID->rowCount();
	}
	
	public function DBlastInsertId()
	{
		return $this->link_ID->lastInsertId();
	}
	
	public function DBcolumnCount()
	{
		return $this->link_ID->columnCount();
	}
	
	public function DBgetColumnMeta($column)
	{
		return $this->link_ID->getColumnMeta($column);
	}
	
	public function DBbeginTransaction()
	{
		$this->inTransaction = TRUE;
		$this->link_ID->beginTransaction();
	}
	
	public function DBcommit()
	{
		if ($this->inTransaction)
		{
			$this->inTransaction = FALSE;
			$this->link_ID->commit();
		}
	}
	
	public function DBrollBack()
	{
		if ($this->inTransaction)
		{
			$this->inTransaction = FALSE;
			$this->link_ID->rollBack();
		}
	}
	
	public function DBqueryOne($sql)
	{
	    $result = $this->query( $sql  , PDO::FETCH_OBJ);
	    
	    return $result->fetchAll(PDO::FETCH_OBJ);
	}
}



